package com.test.ui
{
	import laya.display.Animation;
	import laya.display.Sprite;
	import laya.maths.Rectangle;
	public class Role extends Sprite
	{
		//定义飞机的身体
		public  var  body:Animation;
		
		//是否缓存了动画
		private static  var  cached: Boolean = false;
		
		//定义飞机类型
		private var type: String;
		//阵营,0：我方，1：敌方
		public var camp: Number;
		//血量
		public var hp: Number;
		//飞行速度
		public var speed: Number;
		//攻击半径
		public var hitRadius: Number;
		//射击类型
		public var shootType:Number = 0;
		//射击间隔
		public var shootInterval:Number = 500;
		//下次射击时间
		public var shootTime:Number = Laya.Browser.now() + 2000 ;
		
		//当前动作
		public var action:String;
		//是否是子弹
		public var isBullet:Boolean = false;
		public function Role()
		{
			super();
		}
		
		public  function init(type: String, camp: Number, hp: Number, speed: Number, hitRadius: Number):void
		{
			
			this.type = type;
			this.camp = camp;
			this.hp = hp;
			this.speed = speed;
			this.hitRadius = hitRadius;
			
			//缓存公用动画模板，减少对象创建开销
			if (!Role.cached) {
				Role.cached = true;
				//缓存hero_fly动画
				Animation.createFrames(["war/hero_fly1.png", "war/hero_fly2.png"], "hero_fly");
				//缓存hero_down动画
				Animation.createFrames(["war/hero_down1.png", "war/hero_down2.png", "war/hero_down3.png", "war/hero_down4.png"], "hero_down");
				
				//缓存enemy1_fly动画
				Animation.createFrames(["war/enemy1_fly1.png"], "enemy1_fly");
				//缓存enemy1_down动画
				Animation.createFrames(["war/enemy1_down1.png", "war/enemy1_down2.png", "war/enemy1_down3.png", "war/enemy1_down4.png"], "enemy1_down");
				
				//缓存enemy2_fly动画
				Animation.createFrames(["war/enemy2_fly1.png"], "enemy2_fly");
				//缓存enemy2_down动画
				Animation.createFrames(["war/enemy2_down1.png", "war/enemy2_down2.png", "war/enemy2_down3.png", "war/enemy2_down4.png"], "enemy2_down");
				//缓存enemy2_hit动画
				Animation.createFrames(["war/enemy2_hit.png"], "enemy2_hit");
				
				//缓存enemy3_fly动画
				Animation.createFrames(["war/enemy3_fly1.png", "war/enemy3_fly2.png"], "enemy3_fly");
				//缓存enemy3_down动画
				Animation.createFrames(["war/enemy3_down1.png", "war/enemy3_down2.png", "war/enemy3_down3.png", "war/enemy3_down4.png", "war/enemy3_down5.png", "war/enemy3_down6.png"], "enemy3_down");
				//缓存enemy3_hit动画
				Animation.createFrames(["war/enemy3_hit.png"], "enemy3_hit");
				
				//缓存子弹动画
				Laya.Animation.createFrames(["war/bullet1.png"], "bullet1_fly");
			}
			if(!this.body){
				this.body = new Animation();
				this.body.interval = 50;
				this.addChild(this.body);
				this.body.on("complete",this,this.onPlayComplete);
			}
			this.playAction("fly");
		}
		
		public  function onPlayComplete():void
		{
			if(this.action === "down"){
				this.body.stop();
				this.visible = false;
			}else if(this.action ==="hit")	{
				this.playAction("fly");
			}		
		}
		
		public function playAction(action:String):void{
			this.action = action;
			this.body.play(0,true,this.type + "_"  + action);
			var bound : Rectangle = this.body.getBounds();
			this.body.pos(-bound.width / 2, -bound.height / 2);
		}
		
	}
}